{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://storage.googleapis.com/allennlp-data-bucket/nllb/eng_Latn-zho_Hant.gz\n",
    "# !gzip -d eng_Latn-zho_Hant.gz\n",
    "# !wc -l /home/husein/ssd3/multilang/eng_Latn-zho_Hant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !head -n 2 /home/husein/ssd3/multilang/eng_Latn-zho_Hant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "\n",
    "def language_detection_textcleaning(string):\n",
    "    string = re.sub(\n",
    "        'http\\\\S+|www.\\\\S+',\n",
    "        '',\n",
    "        ' '.join(\n",
    "            [i for i in string.split() if i.find('#') < 0 and i.find('@') < 0]\n",
    "        ),\n",
    "    )\n",
    "\n",
    "    chars = ',.()!:\\'\"/;=-'\n",
    "    for c in chars:\n",
    "        string = string.replace(c, f' {c} ')\n",
    "    string = string.replace('\\n', '').replace('\\t', '')\n",
    "\n",
    "    string = re.sub(\n",
    "        '[0-9!@#$%^&*()_\\\\-+{}|\\\\~`\\'\";:?/.>,<]', ' ', string, flags=re.UNICODE\n",
    "    )\n",
    "    string = re.sub(r'[ ]+', ' ', string).strip()\n",
    "\n",
    "    return string.lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2265456it [00:04, 464255.71it/s]\n"
     ]
    }
   ],
   "source": [
    "with open('standard-mandarin.jsonl', 'w') as fopen_l:\n",
    "    with open('/home/husein/ssd3/multilang/eng_Latn-zho_Hant') as fopen:\n",
    "        for row in tqdm(fopen):\n",
    "            splitted = row.split('\\t')\n",
    "            if float(splitted[2]) >= 1.07:\n",
    "                fopen_l.write(f'{json.dumps(splitted[1])}\\n')\n",
    "            else:\n",
    "                break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import malaya"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning : `load_model` does not return WordVectorModel or SupervisedModel any more, but a `FastText` object which is very similar.\n"
     ]
    }
   ],
   "source": [
    "language_detection = malaya.language_detection.fasttext(model = 'mesolitica/fasttext-language-detection-v1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# local_mandarin = []\n",
    "# with open('/home/husein/ssd3/cn-cari-com-my/everything.jsonl') as fopen:\n",
    "#     for l in tqdm(fopen):\n",
    "#         data = json.loads(l)\n",
    "#         soup = BeautifulSoup(data, \"lxml\")\n",
    "#         texts = [d.text for d in soup.find_all('div')]\n",
    "#         texts = [language_detection_textcleaning(t) for t in texts if t and len(t)]\n",
    "#         texts = [t for t in texts if len(t) and language_detection.predict([t])[0] in {'other'}]\n",
    "#         local_mandarin.extend(texts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14538890 /home/husein/ssd3/c-cari-com-my/everything.jsonl\r\n"
     ]
    }
   ],
   "source": [
    "!wc -l /home/husein/ssd3/c-cari-com-my/everything.jsonl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "14538890it [05:34, 43408.42it/s]\n"
     ]
    }
   ],
   "source": [
    "with open('local-mandarin.jsonl', 'w') as fopen_l:\n",
    "    with open('/home/husein/ssd3/c-cari-com-my/everything.jsonl') as fopen:\n",
    "        for l in tqdm(fopen):\n",
    "            data = json.loads(l)\n",
    "            if language_detection.predict([data])[0] not in {'other'}:\n",
    "                continue\n",
    "            fopen_l.write(f'{json.dumps(data)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8502399 local-mandarin.jsonl\r\n"
     ]
    }
   ],
   "source": [
    "!wc -l local-mandarin.jsonl"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
